﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ST.Buz.Base;
using ST.Buz.Base.Entity;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ST.Buz.Service
{
	public partial interface IServiceSysRoleAction
	{
		/// <summary>
		/// 检验当前用户是否有某项功能权限
		/// </summary>
		/// <param name="sessionCurrent"></param>
		/// <param name="actionCode"></param>
		/// <returns></returns>
		bool ValidateAuth(UserSession sessionCurrent, string actionCode);

		/// <summary>
		/// 检验指定角色是否有某项功能权限
		/// </summary>
		/// <param name="roleID"></param>
		/// <param name="actionCode"></param>
		/// <returns></returns>
		bool ValidateAuth(int roleID, string actionCode);

		/// <summary>
		/// 检验当前用户是否有某些功能权限
		/// </summary>
		/// <param name="sessionCurrent"></param>
		/// <param name="actionCodes"></param>
		/// <returns></returns>
		bool[] ValidateAuth(UserSession sessionCurrent, string[] actionCodes);

		/// <summary>
		/// 检验指定角色是否有某些功能权限
		/// </summary>
		/// <param name="roleID"></param>
		/// <param name="actionCode"></param>
		/// <returns></returns>
		bool[] ValidateAuth(int roleID, string[] actionCodes);
	}

	public partial class ServiceSysRoleAction
	{
		public ServiceSysRoleAction(BuzContext ctx,
			ILogger<ServiceSysRoleAction> log)
		{
			context = ctx;
			logger = log;
		}

		/// <summary>
		/// 检验当前用户是否有某项功能权限
		/// </summary>
		/// <param name="sessionCurrent"></param>
		/// <param name="actionCode"></param>
		/// <returns></returns>
		public bool ValidateAuth(UserSession sessionCurrent, string actionCode)
		{
			if (sessionCurrent == null || sessionCurrent.Roles == null || sessionCurrent.Roles.Length == 0)
			{
				return false;
			}
			return context.SysRoleAction.Any(m => sessionCurrent.Roles.Contains(m.RoleID) && m.ActionCode == actionCode);
		}

		/// <summary>
		/// 检验指定角色是否有某项功能权限
		/// </summary>
		/// <param name="roleID"></param>
		/// <param name="actionCode"></param>
		/// <returns></returns>
		public bool ValidateAuth(int roleID, string actionCode)
		{
			return context.SysRoleAction.Any(m => m.RoleID == roleID && m.ActionCode == actionCode);
		}

		/// <summary>
		/// 检验当前用户是否有某些功能权限
		/// </summary>
		/// <param name="sessionCurrent"></param>
		/// <param name="actionCodes"></param>
		/// <returns></returns>
		public bool[] ValidateAuth(UserSession sessionCurrent, string[] actionCodes)
		{
			if (sessionCurrent == null || sessionCurrent.Roles == null || sessionCurrent.Roles.Length == 0)
			{
				return null;
			}
			if (actionCodes == null || actionCodes.Length == 0)
			{
				return null;
			}
			bool[] results = new bool[actionCodes.Length];
			for (int i = 0; i < actionCodes.Length; i++)
			{
				results[i] = context.SysRoleAction.Any(m => sessionCurrent.Roles.Contains(m.RoleID) && m.ActionCode == actionCodes[i]);
			}
			return results;
		}

		/// <summary>
		/// 检验指定角色是否有某些功能权限
		/// </summary>
		/// <param name="roleID"></param>
		/// <param name="actionCode"></param>
		/// <returns></returns>
		public bool[] ValidateAuth(int roleID, string[] actionCodes)
		{
			if (actionCodes == null || actionCodes.Length == 0)
			{
				return null;
			}
			bool[] results = new bool[actionCodes.Length];
			for (int i = 0; i < actionCodes.Length; i++)
			{
				results[i] = context.SysRoleAction.Any(m => m.RoleID == roleID && m.ActionCode == actionCodes[i]);
			}
			return results;
		}
	}
}